package csc.main;

/**
 * Basic implementation of Thresholder interface.
 * @author Stanislau Dubrouski
 */
class BasicThresholderImpl implements Thresholder {
    
    /**
     * Applies basic thresholding on image. Color intensity of background voxels is set to 0. 
     * @param image 3D list representing input image.
     * @param thresholdLevel minimum color intensity.
     * @return thresholded image. Color intensity of background voxels is set to 0.
     */
    public int[][][] thresholdImage(int[][][] image, int thresholdLevel) {
        for (int z = 0; z < image[0][0].length; z++) {
            for (int x = 0; x < image.length; x++) {
                for (int y = 0; y < image[0].length; y++) {
                    int red = (int) (image[x][y][z] & 0xff0000) >> 16;
                    int green = (int) (image[x][y][z] & 0x00ff00) >> 8;
                    int blue = (int) (image[x][y][z] & 0x0000ff);
                    red = (red < thresholdLevel) ? 0 : red;
                    green = (green < thresholdLevel) ? 0 : green;
                    blue = (blue < thresholdLevel) ? 0 : blue;
                    image[x][y][z] = ((red & 0xff) << 16) + ((green & 0xff) << 8) + (blue & 0xff);
                }
            }
        }

        return image;
    }
}
